package testcase;

import card.*;

import junit.framework.TestCase;

public class CardsetTest extends TestCase {
	private Cardset cardset;
	private Cardset buffer;
	
	/**
	* Sets up the test fixture.
	*
	* Called before every test case method.
	*/
	public void setUp() { 
		//System.setOut(new PrintStream(outContent));
		cardset = new Cardset(2);
		cardset.add(new Card('s', 'A'));
		cardset.add(new Card('h', '2'));
		buffer = new Cardset(cardset);
	}
	
	// Test case 1: add(<Card>) function will return false if the number of cards is out of maximum
	public void testAddCardOverflow(){
		cardset = new Cardset(2);
		assertEquals(cardset.add(new Card()), true);
		assertEquals(cardset.add(new Card()), true);
		assertEquals(cardset.add(new Card()), false);
	}
	
	// Test case 2: add(<Cardset>) function will return false if the number of cards is out of maximum
	public void testAddCardsetOverflow(){
		cardset = new Cardset(3);
		buffer = new Cardset(4);
		buffer.add(new Card());
		buffer.add(new Card());
		buffer.add(new Card());
		assertEquals(cardset.add(buffer), true);
		cardset = new Cardset(3);
		buffer.add(new Card());
		assertEquals(cardset.add(buffer), false);
	}
	
	// Test case 3: takeOut(<int>) function will remove one card and the card could be returned correctly
	public void testTakeOut1Card(){
		Card temp1 = new Card('s', 'A');
		Card temp2 = new Card('h', '2');
		Card temp3 = new Card('g', 'h');
		cardset = new Cardset(3);
		cardset.add(new Card(temp1));
		cardset.add(new Card(temp2));
		cardset.add(new Card(temp3));
		assertEquals(cardset.takeOut(3), null);
		assertEquals(cardset.takeOut(2).compare(temp3), true);;
		assertEquals(cardset.takeOut(1).compare(temp2), true);;
		assertEquals(cardset.takeOut(0).compare(temp1), true);;
		assertEquals(cardset.takeOut(0), null);
	}
	
	// Test case 4: remove(<Card>) function will remove the correct card in cardset
	public void testRemoveCard(){
		cardset = new Cardset(3);
		cardset.add(new Card('s', 'A'));
		cardset.add(new Card('h', '2'));
		cardset.add(new Card('c', '3'));
		Card temp = new Card('s', 'A');
		
		int cnt = 0;
		for(int i=0; i<cardset.count(); i++)
			if(cardset.getCard(i).compare(temp))
				cnt++;
		assertEquals(cnt, 1);
		
		cardset.remove(temp);
		for(int i=0; i<cardset.count(); i++)
			assertEquals(cardset.getCard(i).compare(temp), false);
		
		Card nil = new Card('g', 'B');
		cardset.remove(nil);
	}
	
	// Test case 5: remove(<Cardset>) function will remove the correct cardset
	public void testRemoveCardset(){
		cardset = new Cardset(3);
		cardset.add(new Card('s', 'A'));
		cardset.add(new Card('h', '2'));
		cardset.add(new Card('c', '3'));
		Cardset temp = new Cardset(2);
		temp.add(new Card('s', 'A'));
		temp.add(new Card('h', '2'));
		
		int cnt = 0;
		for(int i=0; i<cardset.count(); i++)
			for(int j=0; j<temp.count(); j++)
				if(cardset.getCard(i).compare(temp.getCard(j)))
					cnt++;
		assertEquals(cnt, 2);
		assertEquals(cardset.count(), 3);
		
		cardset.remove(temp);
		
		assertEquals(cardset.getCard(0).compare(new Card('c', '3')), true);
	}
	
	// Test case 6: test count function, return the number of card of a cardset
	public void testCount(){
		cardset = new Cardset(5);
		for(int i=0; i<5; i++){
			cardset.add(new Card('s', 'A'));
			assertEquals(cardset.count(), i+1);
		}
	}
	
	// Test case 7: test getMax function, return collect maximum number
	public void testGetMax(){
		cardset = new Cardset(5);
		assertEquals(cardset.getMax(), 5);
	}

	// Test case 8: clear the cardset
	public void testClear(){
		cardset = new Cardset(3);
		cardset.add(new Card());
		cardset.add(new Card());
		cardset.add(new Card());
		cardset.clear();
		assertEquals(cardset.count(), 0);
	}
	
	// Test case 9: sort cardset by value
	public void testSort(){
		cardset = new Cardset(5);
		cardset.add(new Card('g','R'));
		cardset.add(new Card('s','2'));
		cardset.add(new Card('h','2'));
		cardset.add(new Card('g','B'));
		cardset.add(new Card('s','5'));
		cardset.sort();
		assertEquals(cardset.getCard(0).getValue(), '5');
		assertEquals(cardset.getCard(1).getValue(), '2');
		assertEquals(cardset.getCard(2).getValue(), '2');
		assertEquals(cardset.getCard(3).getValue(), 'B');
		assertEquals(cardset.getCard(4).getValue(), 'R');
	}
	
	/**
	* Tears down the test fixture.
	*
	* Called after every test case method.
	*/
	public void tearDown() { //System.setOut(null); 
	}
}

